<template>
	<div class="aid">
		<div class="base">
			<p class="fixed-top">
				{{ JSON.stringify(text) == '{}' ? '添加问卷' : '修改问卷' }}
				<a-icon @click="closeFunction" type="close" />
			</p>
			<div class="main" id="new_message">
				<a-form-model ref="ruleForm" :model="form" :rules="rules">
					<a-form-model-item label="名称" prop="Name"><a-input v-model="form.Name" /></a-form-model-item>
					<a-form-model-item label="开始时间" prop="StartTime">
						<a-date-picker v-model:value="form.StartTime" @change="timeChange1($event,1)"/>
					</a-form-model-item>
					<a-form-model-item label="截止时间" prop="EndTime">
						<a-date-picker v-model:value="form.EndTime" @change="timeChange1($event,2)"/>
					</a-form-model-item>
					
					<!-- <a-form-model-item label="每月开始问卷日期" prop="StartDay">
						<a-input-number v-model="form.StartDay" :min="1" :max="31" />
					</a-form-model-item> -->
					<a-form-model-item label="是否全员" prop="IsAll">
						<a-radio-group name="radioGroup" v-model="form.IsAll">
							<a-radio :value="true">是</a-radio>
							<a-radio :value="false">否</a-radio>
						</a-radio-group>
					</a-form-model-item>
					<a-form-model-item label="是否开启" prop="IsEnable">
						<a-radio-group name="radioGroup" v-model="form.IsEnable">
							<a-radio :value="true">是</a-radio>
							<a-radio :value="false">否</a-radio>
						</a-radio-group>
					</a-form-model-item>
					<a-form-model-item label="未问卷是否弹窗提醒" prop="IsPopRemind">
						<a-radio-group name="radioGroup" v-model="form.IsPopRemind">
							<a-radio :value="true">是</a-radio>
							<a-radio :value="false">否</a-radio>
						</a-radio-group>
					</a-form-model-item>
					<a-form-model-item label="未问卷是否隐藏积分" prop="IsHideScore">
						<a-radio-group name="radioGroup" v-model="form.IsHideScore">
							<a-radio :value="true">是</a-radio>
							<a-radio :value="false">否</a-radio>
						</a-radio-group>
					</a-form-model-item>
					<a-form-model-item label="描述" prop="comment"><a-input type="textarea" :auto-size="global.textareaHeight" v-model="form.Content" /></a-form-model-item>
					
					<a-form-model-item label="" class="fixed-bottom">
						<a-button type="primary" @click="handleSubmit">{{ JSON.stringify(text) == '{}' ? '确认添加' : '确认修改' }}</a-button>
						<a-button style="margin-left: 10px;" @click="closeFunction">取消</a-button>
					</a-form-model-item>
				</a-form-model>
			</div>
		</div>
	</div>
</template>

<script>
import moment from 'moment';
import { ManageEvaluationDoAdd, ManageEvaluationDoUpdate, DoGetDetailStudent } from '@/api/follow';
export default {
	props: {
		text: Object,
		ClassList: Array,
		DepartCodeList: Array
	},
	watch: {
		text: function(text) {
			if (JSON.stringify(text) == '{}') {
			} else {
				// this.GetDetailStudent(text)
				this.form = text;
				this.gradeYear = text.Grade;
			}
		}
	},
	data() {
		return {
			gradeYear: null,
			yearPickShow: false,
			loading: false,
			formLayout: 'horizontal',
			form: this.$form.createForm(this, {
				name: 'coordinated'
			}),
			form: {
				Name: '',
				IsEnable: '',
				IsAll:'',
				IsPopRemind: '',
				IsHideScore: '',
				EndTime: '',
				StartTime:"",
				StartDay: '',
				Content:''
			},
			rules: {
				Name: [
					{
						required: true,
						message: '请输入名称',
						trigger: 'change'
					}
				],
				StartTime: [
					{
						required: true,
						message: '请选择开始时间',
						trigger: 'change'
					}
				],
				EndTime: [
					{
						required: true,
						message: '请选择结束时间',
						trigger: 'change'
					}
				],
				StartDay: [
					{
						required: true,
						message: '请选择问卷开始时间',
						trigger: 'change'
					}
				],
				IsEnable: [
					{
						required: true,
						message: '请选择是否开启',
						trigger: 'change'
					}
				],
				IsPopRemind: [
					{
						required: true,
						message: '请选择是否弹窗提醒',
						trigger: 'change'
					}
				],
				IsHideScore: [
					{
						required: true,
						message: '请选择是否隐藏积分',
						trigger: 'change'
					}
				],
				IsAll: [
					{
						required: true,
						message: '请选择是否全员',
						trigger: 'change'
					}
				]
			}
		};
	},
	created() {},
	methods: {
		timeChange1(e, t) {
			if (t === 1) {
				if (moment(e).diff(moment(this.form.EndTime)) > 0) {
					this.$message.error('开始时间不能大于截止时间！')
					this.form.StartTime = ''
					return false
				}
			} else {
				if (moment(e).diff(moment(this.form.StartTime)) < 0) {
					this.$message.error('开始时间不能大于截止时间！')
					this.form.EndTime = ''
					return false
				}
			}
		},
		filterOption(input, option) {
			return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0;
		},
		moment,
		handleOpenChange(status) {
			this.yearPickShow = status;
		},
		handlePanelChange(value, mode) {
			this.gradeYear = moment(value).format('YYYY');
			this.form.Grade = this.gradeYear;
			this.yearPickShow = false;
		},
		yearChange(date, dateString) {
			if (!date) {
				this.gradeYear = null;
				this.form.Grade = this.gradeYear;
			}
		},
		closeFunction(data) {
			this.$emit('closeFun', data);
			this.form = {};
			this.gradeYear = null;
			this.fileList = [];
		},
		handleSubmit(e) {
			this.$refs.ruleForm.validate(async valid => {
				if (valid) {
					let data = this.form;
					data.EndTime = moment(this.form.EndTime).format('YYYY-MM-DD');
					data.StartTime = moment(this.form.StartTime).format('YYYY-MM-DD');
					console.log(data);
					if(data.StartDay<10){
						data.StartDay='0'+data.StartDay
					}
					let res = {};
					if (JSON.stringify(this.text) == '{}') {
						res = await ManageEvaluationDoAdd(data);
					} else {
						res = await ManageEvaluationDoUpdate(data);
					}
					if (res.data.code === 0) {
						this.$message.success(res.data.msg);
						this.closeFunction('1');
					} else {
						this.$message.error(res.data.msg);
					}
				} else {
					return false;
				}
			});
		}
	}
};
</script>

<style lang="less" scoped>
/deep/.ant-form-item {
	margin: 20px 50px;
}
/deep/.ant-form-item-label {
	width: 160px !important;
	overflow: hidden;
	white-space: nowrap;
	text-overflow: ellipsis;
	text-align: right;
}
/deep/.ant-form-item-control-wrapper {
	flex: 1;
}
</style>
